package com.newtouch.bxzs.database.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.model.DataDictionary;
import com.newtouch.bxzs.common.base.model.bo.DataDictionaryValueBO;
import com.newtouch.bxzs.common.base.util.Assert;
import com.newtouch.bxzs.common.base.util.EntityToBeanUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import com.newtouch.bxzs.database.controller.DataDictionaryController;
import com.newtouch.bxzs.database.mapper.DataDictionaryMapper;
import com.newtouch.bxzs.database.model.bo.DataDictionaryBO;
import com.newtouch.bxzs.database.model.bo.DictionaryEntryBO;
import com.newtouch.bxzs.database.model.vo.DataDictionaryVO;
import com.newtouch.bxzs.database.service.DataDictionaryService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * 数据字典管理service接口的实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/5</td>
 * <td>zhangqianyang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author zhangqianyang
 * @version 1.0.0
 * @since 1.0.0
 */

@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

	@Resource
	private DataDictionaryMapper dataDictionaryMapper;

	/**
	 * 静态变量：日志
	 */
	private static final Log logger = LogFactory.getLog(DataDictionaryController.class);

	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.message.service.DataDictionaryService#
	 * getDataDictionarieList(com.newtouch.bxzs.database.model.vo.
	 * DataDictionaryVO)
	 */
	@Override
	public List<DataDictionaryBO> getDataDictionaryList(String dataDictionaryName) {
		/* 实例化QueryWrapper */
		QueryWrapper<DataDictionary> query = new QueryWrapper<>();
		/* 查询状态为启用的 */
		query.eq("IS_ENABLE", true);
		if (!StringUtils.isEmpty(dataDictionaryName)) {
			/* 通过数据字典名称模糊查询 */
			query.like("DATA_DICTIONARY_NAME", dataDictionaryName);
		}
		/* 按照数据字典名称排序 */
		query.orderByAsc("DATA_DICTIONARY_NAME");
		/* 查询出数据字典列表 */
		List<DataDictionary> dataDictionaryList = dataDictionaryMapper.selectList(query);
		return EntityToBeanUtils.copyBean(dataDictionaryList, DataDictionaryBO.class);
	}

	/**
	 * (non-Javadoc)
	 * <p>
	 * <p>
	 * com.newtouch.bxzs.message.service.DataDictionaryService#
	 * modifyDataDictionarie(com.newtouch.bxzs.database.model.vo.
	 * DataDictionaryVO)
	 */
	@Override
	public String modifyDataDictionary(DataDictionaryVO dataDictionaryVO) {
		if (dataDictionaryVO == null || StringUtils.isEmpty(dataDictionaryVO.getDataDictionaryName())) {
			logger.info("必填项为输入");
			throw new BaseException(BaseErrorEnum.REQUEST_DATA_NULL);
		}
		/* 实例化DataDictionary */
		DataDictionary dataDictionary = new DataDictionary();
		/* 使用工具直接进行实体与BO的复制转换，前面是来源，后面是目标 */
		BeanUtil.copyProperties(dataDictionaryVO, dataDictionary);
		dataDictionary.setEnableFlag(true);
		Date date = new Date();
		dataDictionary.setCreateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());
		dataDictionary.setCreateTime(date);
		dataDictionary.setUpdateUserId(Objects.requireNonNull(UserDetailUtil.getUserDetail()).getUserId());
		dataDictionary.setUpdateTime(date);
		dataDictionaryMapper.insert(dataDictionary);
		return "新增数据字典成功";
	}

	/**
	 * 获取所有的字典值，放在缓存中
	 */
	@Override
	public HashMap<String, List<DataDictionaryValueBO>> getAllDataDictionaryEntry() {
		HashMap<String, List<DataDictionaryValueBO>> dictValue = new HashMap<>(16);
		// 查询全部的字典值
		List<DictionaryEntryBO> list = dataDictionaryMapper.getAllDataDictionaryEntry();
		if (BeanUtil.isNotEmpty(list)) {
			for (DictionaryEntryBO dictionaryEntryBO : list) {
				dictValue.put(dictionaryEntryBO.getDataDictionaryCode(),
						dictionaryEntryBO.getDataDictionaryEntryValue());
			}
		}
		return dictValue;
	}

	/**
	 * 根据字典编码获取字典对应的值
	 *
	 * @param dictionaryCode
	 *            字典编码
	 * @return 字典值
	 */
	@Override
	public List<DataDictionaryValueBO> getDataDictionaryEntry(String dictionaryCode) {
		Assert.notEmpty(dictionaryCode, BaseErrorEnum.REQUEST_DATA_NULL.getResultMsg());
		// 根据code去数据库查询
		return dataDictionaryMapper.listDataDictionaryEntryByCode(dictionaryCode);
	}

	/**
	 * 通过数据字典ID获取数据字典编码
	 *
	 * @param dataDictionaryId 数据字典ID
	 * @return 数据字典编码
	 */
	@Override
	public String getDataDictionaryCodeById(String dataDictionaryId) {
		Assert.notEmpty(dataDictionaryId, BaseErrorEnum.REQUEST_PARAMETER_ERROR.getResultMsg());
		DataDictionary dataDictionary = dataDictionaryMapper.selectById(dataDictionaryId);
		if(dataDictionary==null){
			throw new BaseException(BaseErrorEnum.REQUEST_PARAMETER_ERROR);
		}
		return dataDictionary.getDataDictionaryCode();
	}
}
